package com.jvbc.xoso;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import com.jvbc.xoso.R;
import com.jvbc.xoso.bean.RSSItem;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.TextView;


public class ListReaderActivity extends ListActivity {
	private ArrayList<RSSItem> itemList = null;
	private RssListAdapter rssAdapter = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_rss_reader);
				
		itemList = new ArrayList<RSSItem>();
		new RetrieveRSSFeeds().execute();
	}
	// nhan ve du lieu tu rss add vao list data
	public void retrieveRSSFeed(String urlToRSSFed, ArrayList<RSSItem> list) {
		try{
			URL url = new URL(urlToRSSFed);
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();
			XMLReader xmlReader = parser.getXMLReader();
			//own class
			RSSParser theRSSHandler = new RSSParser(list);
			xmlReader.setContentHandler(theRSSHandler);
			
			InputSource is = new InputSource(url.openStream());
			xmlReader.parse(is);
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	public class RetrieveRSSFeeds extends AsyncTask<Void, Void, Void> {
		private ProgressDialog progress = null;
		Intent inCategory = getIntent();
		String linkRss = inCategory.getStringExtra("CategoryName");
		@Override
		protected Void doInBackground(Void... params) {
			retrieveRSSFeed(linkRss, itemList);
			rssAdapter = new RssListAdapter(ListReaderActivity.this,
					R.layout.rss_item, itemList);
			return null;
		}
		@Override
		public void onPreExecute() {
			progress = ProgressDialog.show(ListReaderActivity.this, 
					null, "Loading RSS Feeds...");
			super.onPreExecute();		
		}
		@Override
		protected void onPostExecute(Void result) {
			setListAdapter(rssAdapter);
			progress.hide();
			super.onPostExecute(result);
		}
		@Override
		protected void onProgressUpdate(Void... values) {
			super.onProgressUpdate(values);
		}
	}
	private class RssListAdapter extends ArrayAdapter<RSSItem> {
		private List<RSSItem> data = null;
		
		public RssListAdapter(Context context, int textViewResourceId, 
				List<RSSItem> data) {
			super(context, textViewResourceId, data);
			this.data = data;
		}
		@Override
		public int getCount() {
			return (data != null)? data.size():0;
		}
		@Override
		public long getItemId(int position) {
			return position;
		}
		@Override
		public RSSItem getItem(int position) {
			return (data !=null) ? data.get(position) : null;
		}
		@Override
		public View getView(int position, View convertView, 
				ViewGroup parent) {
			View view = convertView;
			if (view == null) {
				LayoutInflater row = (LayoutInflater)
						ListReaderActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = row.inflate(R.layout.rss_item, null);
			} 
			RSSItem item = data.get(position);
			if (item !=null) {
				TextView title = (TextView)view.findViewById(R.id.title);
				TextView date = (TextView)view.findViewById(R.id.pubDate);
				WebView description = (WebView)view.findViewById(R.id.description);
				
				title.setText(item.getTitle());
				date.setText(item.getPubDate());
				
				description.getSettings().setDefaultTextEncodingName("utf-8");
				description.loadData(item.getDescription(), "text/html; charset=utf-8", "utf-8");
			}
			return view;
		}
	}
	
	
}
